package qlsl.androiddesign.http.service.subservice;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import qlsl.androiddesign.constant.WhatConstant;
import qlsl.androiddesign.entity.commonentity.Pager;
import qlsl.androiddesign.entity.otherentity.Category.RowsBean;
import qlsl.androiddesign.entity.otherentity.ShoopChildList;
import qlsl.androiddesign.handler.subhandler.HttpHandler;
import qlsl.androiddesign.http.HttpListener;
import qlsl.androiddesign.http.service.baseservice.BaseService;
import qlsl.androiddesign.http.xutils.HttpProtocol;
import qlsl.androiddesign.thread.HandlerThread;
import qlsl.androiddesign.util.commonutil.Log;
import qlsl.androiddesign.util.commonutil.PagerUtils;
import qlsl.androiddesign.view.baseview.FunctionView;
import android.widget.ListView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * **模块 <br/>
 * 
 */
public class ShoppingService extends BaseService {

	private static String className = getClassName(ShoppingService.class);

	public static void queryCatagoryList(final FunctionView<?> functionView,
			final HttpListener listener) {
		functionView.setProgressBarText("正在获取分类列表");
		final HttpHandler handler = getHandler(functionView, listener,
				className, "queryCatagoryList");
		new HandlerThread(className, "queryCatagoryList") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {

					JSONObject jo = protocol.setMethod(
							"getsuppliergoodstypelist").post();

					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}

					List<RowsBean> list = JSONArray.parseArray(
							jo.getString("rows"), RowsBean.class);

					HashMap<String, Object> map = new HashMap<String, Object>();
					ArrayList<HashMap<String, RowsBean>> groupList = new ArrayList<HashMap<String, RowsBean>>();
					ArrayList<ArrayList<HashMap<String, RowsBean>>> childList = new ArrayList<ArrayList<HashMap<String, RowsBean>>>();

					for (RowsBean group : list) {
						HashMap<String, RowsBean> map_group = new HashMap<String, RowsBean>();
						if (group.getSuppliertype().contains("积分兑换")) {
							continue;
						}
						map_group.put("group", group);

						groupList.add(map_group);

						List<RowsBean> groupChildList = new ArrayList<RowsBean>();
						ArrayList<HashMap<String, RowsBean>> currentChildList = new ArrayList<HashMap<String, RowsBean>>();

						for (RowsBean child : list) {
							HashMap<String, RowsBean> map_child = new HashMap<String, RowsBean>();
							map_child.put("child", child);
							// currentChildList.add(map_child);
						}

						childList.add(currentChildList);
					}

					Pager pager = PagerUtils.createPager(1, jo);
					map.put("groupList", groupList);
					map.put("childList", childList);
					map.put("pager", pager);

					handler.sendMessage(protocol,
							WhatConstant.WHAT_NET_DATA_SUCCESS, map);
				} catch (Exception e) {
					Log.e(e);
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON,
							e);
				}
			};
		}.start();
	}

	/**
	 * 获取二级三级分类数据
	 * 
	 * @param supplierid
	 *            id
	 * @param id
	 * @param functionView
	 * @param listener
	 */
	public static void queryCatagoryListChild(
			final ListView listView,
			final ArrayList<HashMap<String, ShoopChildList.RowsBean>> childList,
			final String supplierid, final int id,
			final FunctionView<?> functionView, final HttpListener listener) {
		listView.setEnabled(false);
		functionView.setProgressBarText("正在获取分类列表");
		final HttpHandler handler = getHandler(functionView, listener,
				className, "queryCatagoryListChild");
		new HandlerThread(className, "queryCatagoryListChild") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					String url = null;
					switch (id) {
					case 0:
						url = "getsubtiergoodstypelist";
						break;
					case 1:
						url = "getsumciergoodstypelist";
						break;
					}
					JSONObject jo = protocol.setMethod(url)
							.addParam("supplierid", supplierid).post();

					if (isDataInvalid(jo, handler, protocol)) {
						functionView.activity.runOnUiThread(new Runnable() {

							public void run() {
								listView.setEnabled(true);
							}
						});
						return;
					}

					List<ShoopChildList.RowsBean> list = JSONArray
							.parseArray(jo.getString("rows"),
									ShoopChildList.RowsBean.class);

					// HashMap<String, Object> map = new HashMap<String,
					// Object>();
					//
					// ArrayList<HashMap<String, RowsBean>> groupList = new
					// ArrayList<HashMap<String, RowsBean>>();
					//
					// ArrayList<ArrayList<HashMap<String, RowsBean>>> childList
					// = new ArrayList<ArrayList<HashMap<String, RowsBean>>>();
					//
					// ArrayList<HashMap<String, RowsBean>> currentChildList =
					// new ArrayList<HashMap<String, RowsBean>>();
					//
					for (ShoopChildList.RowsBean child : list) {
						HashMap<String, ShoopChildList.RowsBean> map_child = new HashMap<String, ShoopChildList.RowsBean>();
						map_child.put("child", child);
						childList.add(map_child);
					}
					//
					// childList.add(currentChildList);
					//
					// Pager pager = PagerUtils.createPager(2, jo);
					// map.put("groupList", groupList);
					// map.put("childList", childList);
					// map.put("pager", pager);

					functionView.activity.runOnUiThread(new Runnable() {

						public void run() {
							listView.setEnabled(true);
						}
					});

					handler.sendMessage(protocol,
							WhatConstant.WHAT_NET_DATA_SUCCESS, "");
				} catch (Exception e) {
					Log.e(e);
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON,
							e);
				}
			};
		}.start();
	}

}
